package cn.qdhlm.collectservice.core.component;

import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;
import cn.qdhlm.collectservice.core.component.analyse.*;
import cn.qdhlm.collectservice.model.rocketmq.TerminalData;
import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Name: Analyser
 * @Description: Analyser
 * @Author: jiangcs
 */
@Log4j2
@Component
public class Analyser {
    @Autowired
    private AnyAfn00 anyAfn00;
    @Autowired
    private AnyAfn0a anyAfn0a;
    @Autowired
    private AnyAfn05 anyAfn05;
    @Autowired
    private AnyAfn0c anyAfn0c;
    @Autowired
    private AnyAfna0 anyAfna0;
    @Autowired
    private Commander commander;
    
    @Autowired
    private UtilityService utilityService;



    /**
     * 解析报文
     *
     * @param deviceDataId
     * @param byteBuf
     */
    public void analysis(String deviceDataId, ByteBuf byteBuf,boolean handwork) {
        log.info("analysis[{}] begin ...", deviceDataId);

        // 跳过4个字节
        byteBuf.skipBytes(4);
        // 控制码
        String controlCode = CodeUtil.byteToHexString(byteBuf.readByte());
        // 协议码：0-255
        int codeCustomNB = byteBuf.readByte();
        // 表地址：8位，不足前补0
        String macAddr = ComUtil.fillZeroLeft(CodeUtil.intToHexString(byteBuf.readInt()), 8);

        log.info("analysis: controlCode[{}], codeCustomNB[{}], macAddr[{}]", controlCode, codeCustomNB, macAddr);
        
        // 功能码：AFN
        String afn = CodeUtil.byteToHexString(byteBuf.readByte());
        // 帧序号：SEQ，默认FF
        String seq = CodeUtil.byteToHexString(byteBuf.readByte());
        // 数据单元标识：FN
        int fn = byteBuf.readByte();
        // 数据包标识
        int sn = byteBuf.readUnsignedByte();

        // 协议
        String protocolCode = utilityService.getProtocolCode(codeCustomNB);
        int protocolType = utilityService.getProtocolType(protocolCode);
        String terminalNo = utilityService.getTerminalNo(protocolType, protocolCode, null, macAddr);

        // TerminalData
        TerminalData terminalData = new TerminalData();
        terminalData.setCodeCustomNB(codeCustomNB);
        terminalData.setTerminalNo(terminalNo);
        terminalData.setProtocolType(protocolType);
        terminalData.setProtocolCode(protocolCode);
        terminalData.setMacAddr(macAddr);
        boolean analysisFlag = false;
        switch (afn) {
            // 确认/否认（AFN=00H）
            case "00":
                analysisFlag = anyAfn00.execute(fn, sn, terminalNo);
                break;
            // 查询参数（AFN=05H）
            case "05":
                analysisFlag = anyAfn05.execute(fn, sn, byteBuf, terminalNo);
                break;
            // 1类数据（AFN=0AH）
            case "0a":
                analysisFlag = anyAfn0a.execute(fn, byteBuf, terminalData);
                break;
            // 控制（AFN=0CH）
            case "0c":
                analysisFlag = anyAfn0c.execute(fn, sn, byteBuf, terminalData);
                break;
            // 主动上报实时参数（AFN=A0）
            case "a0":
                analysisFlag = anyAfna0.execute(fn, sn, byteBuf, terminalData);
                break;
            default:
                log.error("analysis: afn[{}] match fail", afn);
        }
        terminalData.setAnalysisResult(""+analysisFlag);
    }
}
